#include "stdafx.h"
#include "ChessKnight.h"

HRESULT ChessKnight::DrawCanMove(const HWND& hwnd)
{
    HDC hdc = GetDC(hwnd);
    HPEN move_pen = CreatePen(PS_SOLID, 1, RGB(0xFF, 0x0, 0x0));
    SelectObject(hdc, move_pen);
    size_t size = m_can_move.size();
    for (int index = 0; index < size; ++index)
    {
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawRectangle(hdc, m_can_move[index]))
    }

    return S_OK;
}


HRESULT ChessKnight::MoveCheck(const CHESS_POS& m_select_pos)
{
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_NULL(m_chess_board.get())

    m_can_move.clear();

    CHESS_POS temp_pos;
    temp_pos.col_index = m_chess_pos.col_index - 1;
    temp_pos.row_index = m_chess_pos.row_index - 2;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index + 1;
    temp_pos.row_index = m_chess_pos.row_index - 2;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index + 2;
    temp_pos.row_index = m_chess_pos.row_index - 1;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index + 2;
    temp_pos.row_index = m_chess_pos.row_index + 1;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index + 1;
    temp_pos.row_index = m_chess_pos.row_index + 2;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index - 1;
    temp_pos.row_index = m_chess_pos.row_index + 2;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index - 2;
    temp_pos.row_index = m_chess_pos.row_index + 1;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    temp_pos.col_index = m_chess_pos.col_index - 2;
    temp_pos.row_index = m_chess_pos.row_index - 1;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(CheckPos(temp_pos))

    return S_OK;
}

HRESULT ChessKnight::CheckPos(const CHESS_POS& pos)
{
    if ((pos.row_index < 0 || pos.row_index > GC_CHESS_BOARD_ROW_COUNT - 1)
        || (pos.col_index < 0 || pos.col_index > GC_CHESS_BOARD_COL_COUNT - 1))
    {
        return S_FALSE;
    }

    PLAYER player = e_no_player;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(m_chess_board->GetChessFillByChessPos(pos, player))
    if (m_type_name == 1)
    {
        if (player != e_player_1)
        {
            m_can_move.push_back(pos);
        }
    }
    else
    {
        if (player != e_player_2)
        {
            m_can_move.push_back(pos);
        }
    }

    return S_OK;
}